﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using VIP.Extension;

namespace VIP {

    /// <summary>
    /// 需要挂在对象上
    /// </summary>
    public class VIPVideo : VIPMonoSingleton<VIPVideo>
    {

        public VideoPlayer videoPlayer;
        public string url;
        public VIPPathEnum vipPathEnum = VIPPathEnum.NONE;

        /// <summary>
        /// 记录更新的帧数
        /// </summary>
        private long updateFrameTime = 0;

        void Update()
        {
            if (videoPlayer.IsNotNull())
            {
                //如果初始化完成
                if (videoPlayer.isPrepared)
                {
                    updateFrameTime = SecondToFrame(Progress, FrameRate);
                }
            }
        }

        /// <summary>
        /// 帧速率
        /// </summary>
        public float FrameRate
        {
            get
            {
                return videoPlayer.frameRate;
            }
        }

        /// <summary>
        /// 获取视频时长（秒）
        /// </summary>
        public float Length
        {
            get
            {
                //如果初始化未完成
                if (!videoPlayer.isPrepared) return 0;
                long count = 0;
                float second = 0;
                bool bo = long.TryParse(videoPlayer.frameCount.ToString(), out count);
                if (bo)
                {
                    second = FrameToSecond(count, FrameRate);
                }
                return second;
            }
        }

        /// <summary>
        /// 实时进度（秒）
        /// </summary>
        public float Progress
        {
            get
            {
                //如果初始化未完成
                if (!videoPlayer.isPrepared) return 0;
                float progress = 0;
                progress = FrameToSecond(videoPlayer.frame, FrameRate);
                return progress;
            }
        }

        /// <summary>
        /// 根据url播放视频
        /// </summary>
        /// <param name="url"></param>
        public void Play()
        {
            string path = VIPPath.GetPath(vipPathEnum);
            if (videoPlayer.IsNotNull())
            {
                if (updateFrameTime > 0)
                {
                    videoPlayer.frame = updateFrameTime;
                    videoPlayer.Play();
                }
                else
                {
                    videoPlayer.source = VideoSource.Url;
                    videoPlayer.url = path + url;
                    //初始化
                    videoPlayer.Prepare();
                    //初始化完成
                    videoPlayer.prepareCompleted += VideoPlay;
                    videoPlayer.loopPointReached += PlayerCompleted;
                }
            }
        }

        /// <summary>
        /// 初始化完成回调
        /// </summary>
        /// <param name="source"></param>
        private void VideoPlay(VideoPlayer videoPlayer)
        {
            videoPlayer.Play();
        }

        /// <summary>
        /// 播放完成回调
        /// </summary>
        /// <param name="videoPlayer"></param>
        private void PlayerCompleted(VideoPlayer videoPlayer)
        {
            Debug.Log("播放完成");
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            videoPlayer.Pause();
        }

        /// <summary>
        /// 停止播放
        /// </summary>
        public void Stop()
        {
            if (videoPlayer.IsNotNull())
            {
                videoPlayer.Stop();
                updateFrameTime = 0;
            }
        }

        /// <summary>
        /// 更新进度
        /// </summary>
        /// <param name="frameTime"></param>
        public void UpdateProgress(float second)
        {
            if (videoPlayer.IsNotNull())
            {
                //如果初始化完成
                if (videoPlayer.isPrepared)
                {
                    updateFrameTime = SecondToFrame(second, FrameRate);
                    videoPlayer.frame = updateFrameTime;
                    videoPlayer.Play();
                }
            }
        }

        /// <summary>
        /// 帧转秒
        /// </summary>
        /// <param name="frame">帧</param>
        /// <param name="rate">帧速率</param>
        /// <returns></returns>
        private float FrameToSecond(long frame, float rate)
        {
            float second = frame / rate;
            return second;
        }

        /// <summary>
        /// 秒转帧
        /// </summary>
        /// <param name="second">秒</param>
        /// <param name="rate">帧速率</param>
        /// <returns></returns>
        private long SecondToFrame(float second, float rate)
        {
            long frame = (long)(second * rate);
            return frame;
        }

    }

}
